package cc.rengu.redp.bizimpl.igas.service.impl;

import cc.rengu.redp.bizimpl.igas.dao.PSrcTxnAuthCfgMapper;
import cc.rengu.redp.bizimpl.igas.domain.PSrcTxnAuthCfg;
import cc.rengu.redp.bizimpl.igas.service.PSrcTxnAuthCfgService;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpSysParamService;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.Common;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.exception.RedpException;
import cc.rengu.redp.common.utils.SortUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 刘玉亮.LL
 * @since 2021-06-09
 */
@DS("igas")
@Service
public class PSrcTxnAuthCfgServiceImpl extends ServiceImpl<PSrcTxnAuthCfgMapper, PSrcTxnAuthCfg> implements PSrcTxnAuthCfgService {

    @Autowired
    private IUpmpSysParamService upmpSysParamService;

    @Override
    public List<PSrcTxnAuthCfg> findPSrcTxnAuthCfgByList(String instId, String msgSrcId, String transChannelId, String txnNum, String bizType) {
        LambdaQueryWrapper<PSrcTxnAuthCfg> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(instId)) {
            wrapper.eq(PSrcTxnAuthCfg::getInstId, instId);
        }
        if (StringUtils.isNotBlank(msgSrcId)) {
            wrapper.eq(PSrcTxnAuthCfg::getMsgSrcId, msgSrcId);
        }
        if (StringUtils.isNotBlank(transChannelId)) {
            wrapper.eq(PSrcTxnAuthCfg::getTransChannelId, transChannelId);
        }
        if (StringUtils.isNotBlank(txnNum)) {
            wrapper.eq(PSrcTxnAuthCfg::getTxnNum, txnNum);
        }
        if (StringUtils.isNotBlank(bizType)) {
            wrapper.eq(PSrcTxnAuthCfg::getBizType, bizType);
        }
        return this.list(wrapper);
        }

    @Override
    public void deletePSrcTxnAuthCfg(String instId, String msgSrcId, String transChannelId) throws  RedpException {
        if (StringUtils.isBlank(instId) || StringUtils.isBlank(msgSrcId) || StringUtils.isBlank(transChannelId)) {
            throw new RedpException("查询交易权限配置唯一索引列条件为空");
        }
        LambdaQueryWrapper<PSrcTxnAuthCfg> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PSrcTxnAuthCfg::getInstId, instId);
        wrapper.eq(PSrcTxnAuthCfg::getMsgSrcId, msgSrcId);
        wrapper.eq(PSrcTxnAuthCfg::getTransChannelId, transChannelId);
        this.baseMapper.delete(wrapper);
    }
    @Override
    public void insertPSrcTxnAuthCfg(PSrcTxnAuthCfg pSrcTxnAuthCfg){
        this.save(pSrcTxnAuthCfg);
    }

    @Override
    public Map<String, Object> srcTxnInfoList(QueryRequest request, String q) {
        request.setPageSize(1000);
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        q = q.replaceAll("&amp;", "&");
        LambdaQueryWrapper<PSrcTxnAuthCfg> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (null == q) {
            return null;
        }
        String instId = "";
        String param1 = null;
        String[] param = q.split("&");
        if (null != param && param.length > 0) {
            param1 = param[0];
        }
        if (null != param && param.length > 1) {
            if (Common.isBlank(param[1]) && Common.isNotBlank(user.getInstId())) {
                instId = user.getInstId();
            } else {
                if (user.isAdmin()) {
                    instId = param[1];
                } else {
                    instId = user.getInstId();
                }
            }
        } else {
            instId = user.getInstId();
        }
        lambdaQueryWrapper.isNotNull(PSrcTxnAuthCfg::getTxnName);
        lambdaQueryWrapper.eq(PSrcTxnAuthCfg::getInstId,instId);
        lambdaQueryWrapper.likeRight(PSrcTxnAuthCfg::getTxnNum,"P");
        lambdaQueryWrapper.eq(PSrcTxnAuthCfg::getTransChannelId,"UPMP");

        if (!StringUtils.isBlank(param1)) {
            lambdaQueryWrapper.like(PSrcTxnAuthCfg::getTxnName, param1);
        }
        Page<PSrcTxnAuthCfg> page = new Page<>();
        SortUtil.handlePageSort(request, page, true);
        Page<PSrcTxnAuthCfg> page1 = this.page(page, lambdaQueryWrapper);
        List<PSrcTxnAuthCfg> listx = page1.getRecords();
        Map<String, Object> maps = new HashMap<String, Object>();
        Map<String, String> map = new HashMap<String, String>();
        List<Object> list = new ArrayList<Object>();
        if (listx != null) {
            for (int i = 0; i < listx.size(); i++) {
                PSrcTxnAuthCfg cfg = listx.get(i);
                map = new HashMap<String, String>();
                map.put("name", cfg.getTxnName());
                map.put("id", cfg.getTxnNum());
                list.add(map);
            }
            maps.put("results", list);
            maps.put("total", page.getTotal());
            int pageIndex = Common.isBlank(request.getPageNum()) ? 1 : request.getPageNum();
            if (pageIndex == page1.getTotal()) {
                maps.put("more", false);
            } else {
                maps.put("more", true);
            }
        }
        return maps;
    }
}
